package cn.hrlly.tao2shou.callback;

import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.Callback;

import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.Commodity;
import cn.hrlly.tao2shou.domain.Status;
import cn.hrlly.tao2shou.listener.OnPublishCommodityListener;
import okhttp3.Request;
import okhttp3.Response;

/**
 * User: gengliang
 * Date: 2016-01-27
 * Time: 11:39
 */

public class PublishCommodityCallback extends Callback<Status>{

    private OnPublishCommodityListener onPublishCommodityListener;

    public PublishCommodityCallback(OnPublishCommodityListener onPublishCommodityListener){
        this.onPublishCommodityListener=onPublishCommodityListener;
    }

    @Override
    public Status parseNetworkResponse(Response response) throws Exception {
        String string=response.body().string();
        Status status=new Gson().fromJson(string,Status.class);
        return status;
    }

    @Override
    public void onError(Request request, Exception e) {
        onPublishCommodityListener.publishCommodityFail(R.string.network_connection_error);
    }

    @Override
    public void onResponse(Status status) {
        if(status.getStatus()==0)
            onPublishCommodityListener.publishCommoditySucc(R.string.publish_commodity_success);
        else
            onPublishCommodityListener.publishCommodityFail(status.getErrorMessage());
    }
}
